package main

import (
	"fmt"
	"github.com/go-redis/redis"
	"time"
)

func main() {

	client := redis.NewClient(&redis.Options{
		Addr:     "162.14.70.254:6379",
		Password: "123456",
		DB:       2,
	})

	// 读的操作，使用.Result()获取结果，包含Error
	// 写的操作，使用.Err()获取结果，并进行判断
	// 设置key的值
	err := client.Set("key01", "value01", 240*time.Second).Err()
	if err != nil {
		fmt.Println("set key01 failed, err:", err)
		return
	}
	// 获取key的值
	fmt.Println(client.Get("key01").Val())

	duration, err := client.TTL("key01").Result()
	if err != nil {
		fmt.Println("get key01 failed, err:", err)
		return
	}
	fmt.Println("ttl info:", duration)

	// 设置一个key01的值，并返回这个key01的旧值
	oldVal, _ := client.GetSet("key01", "value01-ne").Result()
	fmt.Println("key01 oldVal:", oldVal)

	// 批量获取key值
	vals, _ := client.MGet("key01", "key03").Result()
	for _, val := range vals {
		fmt.Println(val)
	}

	client.SetNX("key03", "value03", 0)
	client.SetNX("key04", "value04", 0)
	fmt.Println(client.Get("key03").Val())

	var03, _ := client.IncrBy("key-n", 2).Result()
	fmt.Println(var03)
	var04, _ := client.IncrByFloat("key-n", 3.5).Result()
	fmt.Println(var04)

	var05, _ := client.DecrBy("key-n", 5).Result()
	fmt.Println(var05)
}
